package com.zupig.colormetricdemo;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.SeekBar;

import com.zupig.colormetricdemo.View.ImageHelper;

public class MainActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener {

    protected ImageView iv;
    protected SeekBar sbHue,sbSaturation,sbLum;
    private Bitmap bitmap;
    public static final int MAX_VALUE = 255;
    public static final int MID_VALUE = 127;
    float mHue,saturation,lum;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        iv = (ImageView) findViewById(R.id.iv);
        sbHue = (SeekBar) findViewById(R.id.sb_hue);
        sbSaturation = (SeekBar) findViewById(R.id.sb_saturation);
        sbLum = (SeekBar) findViewById(R.id.sb_lum);
        sbHue.setOnSeekBarChangeListener(this);
        sbSaturation.setOnSeekBarChangeListener(this);
        sbLum.setOnSeekBarChangeListener(this);
        bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.icon);
        sbHue.setMax(MAX_VALUE);
        sbHue.setProgress(MID_VALUE);
        sbSaturation.setMax(MAX_VALUE);
        sbSaturation.setProgress(MID_VALUE);
        sbLum.setMax(MAX_VALUE);
        sbLum.setProgress(MID_VALUE);
        iv.setImageBitmap(bitmap);
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        switch (seekBar.getId()){
            case R.id.sb_hue:
                mHue = (progress-MID_VALUE)*1.0f/MID_VALUE/180;
                break;
            case R.id.sb_saturation:
                saturation = progress*1.0f/MID_VALUE;
                break;
            case R.id.sb_lum:
                lum = progress*1.0f/MID_VALUE;
                break;
        }
        iv.setImageBitmap(ImageHelper.handleImageEffect(bitmap,mHue,saturation,lum));
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }


}
